package com.swm.yzgrtk.utils;

import com.fazecast.jSerialComm.SerialPort;


/**
 * @author 师文明
 * @data 2025/1/15 下午3:32
 **/
public class ComUtil {
    public static void main(String[] args) {

        SerialPort comPort = SerialPort.getCommPort("COM3"); // 替换为您的串口名称
        comPort.setComPortParameters(9600, 8, 1, 0); // 设置串口参数
        comPort.setComPortTimeouts(SerialPort.TIMEOUT_READ_SEMI_BLOCKING, 0, 0);

        if (comPort.openPort()) {
            try {
                String atCommand = "AT\r\n"; // AT指令
                comPort.writeBytes(atCommand.getBytes(), atCommand.getBytes().length);

                byte[] buffer = new byte[1024];
                int bytesRead = comPort.readBytes(buffer, buffer.length);
                if (bytesRead > 0) {
                    String response = new String(buffer, 0, bytesRead);
                    System.out.println("Response: " + response);
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                comPort.closePort(); // 关闭串口
            }
        } else {
            System.out.println("Unable to open serial port!");
        }
    }






}
